{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Program Files\\Anaconda3\\envs\\lm\\lib\\site-packages\\tqdm\\auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import torch\n",
    "import re\n",
    "torch.cuda.is_available()\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(device)\n",
    "\n",
    "from sklearn.metrics import accuracy_score, precision_recall_fscore_support,matthews_corrcoef,roc_auc_score\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "\n",
    "from transformers import AutoTokenizer, Trainer, TrainingArguments, AutoModelForSequenceClassification, AdamW,BertForSequenceClassification,BertModel\n",
    "from tensorflow import keras\n",
    "# load appropriate tokenizer and fine-tuned model\n",
    "tokenizer = AutoTokenizer.from_pretrained('Rostlab/prot_bert_bfd', do_lower_case=False)\n",
    "model = BertForSequenceClassification.from_pretrained(\"model/fold1\")\n",
    "\n",
    "with open('test.csv') as l:\n",
    "  with open('7.16pred.txt','w') as r:\n",
    "    with open('7.16prob.txt','w') as f:\n",
    "      lines=l.readlines()\n",
    "      for line in lines:\n",
    "        input_seq=line.split(',')[1]\n",
    "        input_seq_spaced = ' '.join([ input_seq[i:i+1] for i in range(0, len(input_seq), 1) ])\n",
    "        input_seq_spaced = re.sub(r'[UZOB]', 'X', input_seq_spaced)\n",
    "        input_seq_tok = tokenizer(input_seq_spaced, return_tensors = 'pt')\n",
    "        output = model(**input_seq_tok)\n",
    "        logits = output[0]\n",
    "        y_prob = torch.sigmoid(logits)[:,1].detach().numpy()\n",
    "        f.write(str(y_prob)[1:-1])\n",
    "        f.write('\\n')\n",
    "        y_pred = y_prob > 0.5\n",
    "        if y_pred == True:\n",
    "          input_class ='1'\n",
    "        else:\n",
    "          input_class ='0'\n",
    "        r.write(input_class)\n",
    "        r.write('\\n')\n",
    "\n",
    "      \n",
    "  \n",
    "      \n",
    "    \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
